package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.entity.Student;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.vo.StudentVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

	//1. 根据关键字查询学生
	@Override
	public List<Student> findByWords(StudentVo vo) {
		//1.1 构造查询条件对象并得到查询结果
		return this.lambdaQuery()
					.like(StrUtil.isNotBlank(vo.getSname()),Student::getSname,vo.getSname())
				    .like(StrUtil.isNotBlank(vo.getAddr()),Student::getAddr,vo.getAddr())
					.eq(vo.getCid() != null && vo.getCid() != 0,Student::getCid,vo.getCid())
				    .list();
	}

	//2. 使用lamba进行分页查询
	@Override
	public Page<Student> findByPage(int page, int pageSize) {
		return this.lambdaQuery().page(new Page<>(page,pageSize));
	}

	//3. 条件查询带分页功能
	@Override
	public Page<Student> search(int page, int pageSize, StudentVo vo) {
		return this.lambdaQuery()
				.like(StrUtil.isNotBlank(vo.getSname()),Student::getSname,vo.getSname())
				.like(StrUtil.isNotBlank(vo.getAddr()),Student::getAddr,vo.getAddr())
				.eq(vo.getCid() != null && vo.getCid() != 0,Student::getCid,vo.getCid())
				.page(Page.of(page,pageSize));
	}


}
